package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.base.CommonCascader;
import com.uwlaser.service.DataCourseParamService;
import com.uwlaser.service.SpcMainService;
import com.uwlaser.service.dto.DataCourseParamQueryParam;
import com.uwlaser.service.dto.SpcMainDto;
import com.uwlaser.service.dto.SpcMainQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author mx
 * @date 2022-08-19
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "SPC统计图管理")
@RequestMapping("/api/spcMain")
public class SpcMainController {

    private final SpcMainService spcMainService;

    private final DataCourseParamService dataCourseParamService;

    @GetMapping
    @Log("查询SPC统计图")
    @ApiOperation("查询SPC统计图")
    @PreAuthorize("@el.check('statisticalSpc:list')")
    public ResponseEntity query(SpcMainQueryParam query, Pageable pageable) {
        return new ResponseEntity<>(spcMainService.queryAll(query, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增SPC统计图")
    @ApiOperation("新增SPC统计图")
    @PreAuthorize("@el.check('statisticalSpc:add')")
    public ResponseEntity create(@Validated @RequestBody SpcMainDto resources) {
        return new ResponseEntity<>(spcMainService.insert(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改SPC统计图")
    @ApiOperation("修改SPC统计图")
    @PreAuthorize("@el.check('statisticalSpc:edit')")
    public ResponseEntity update(@Validated @RequestBody SpcMainDto resources) {
        spcMainService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除SPC统计图")
    @ApiOperation("删除SPC统计图")
    @PreAuthorize("@el.check('statisticalSpc:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        spcMainService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("getEquipment")
    @Log("获取设备级联信息")
    @ApiOperation("获取设备级联信息")
    public List<CommonCascader> getEquipment() {
        return spcMainService.getEquipment();
    }

    @PostMapping("getTestItem")
    @Log("获取检测项目")
    @ApiOperation("获取检测项目")
    public ResponseEntity getTestItem(@RequestBody String equipmentCode) {
        return new ResponseEntity<>(spcMainService.getTestItem(equipmentCode), HttpStatus.OK);
    }

    @PostMapping("getDevicename")
    @Log("获取设备信息")
    @ApiOperation("获取设备信息")
    public ResponseEntity getDevicename(@RequestBody String productCode) {
        return new ResponseEntity<>(spcMainService.getDevicename(productCode), HttpStatus.OK);
    }


    @PostMapping("getDatalist")
    @Log("获取质量数据")
    @ApiOperation("获取质量数据")
    public ResponseEntity getDatalist(@RequestBody DataCourseParamQueryParam param) {
//        param.setProductCode("SD-Cell");
//        param.setDeviceCode("SD-DEV0010");
//        param.setParamCode("resistance");
        return new ResponseEntity<>(dataCourseParamService.queryAll(param), HttpStatus.OK);
    }


    @PostMapping("getRedact")
    @Log("获取spc编辑数据")
    @ApiOperation("获取spc编辑数据")
    public ResponseEntity getRedact(@RequestBody Integer id) {
        return new ResponseEntity<>(spcMainService.getRedact(id), HttpStatus.OK);
    }


    @PostMapping("getSpc")
    @Log("获取spc数据")
    @ApiOperation("获取spc数据")
    public ResponseEntity getSpc(@RequestBody Integer id) {
        return new ResponseEntity<>(spcMainService.getSpc(id), HttpStatus.OK);
    }













    /*
    @Log("导出SPC统计图")
    @ApiOperation("导出SPC统计图")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('spcMain:list')")
    public void download(HttpServletResponse response, SpcMainQueryParam query) throws IOException {
        spcMainService.download(spcMainService.queryAll(query), response);
    }*/

}
